package com.upcode.weighing.admin.utils;

import cn.hutool.core.util.ObjectUtil;
import com.upcode.weighing.util.ops.OtherUtills;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ExportFileNameUtils {

    private  Map<String,Integer> typeMap ;

    private static ExportFileNameUtils instance;

    ExportFileNameUtils(){
        typeMap = new HashMap<>();
        //type 类型 1:今日 2:昨日 3:最近7天 4:最近30天
        typeMap.put("1",0);
        typeMap.put("2",-1);
        typeMap.put("3",-7);
        typeMap.put("4",-30);
    }

    public static ExportFileNameUtils getInstance(){
        if(instance == null){
            instance = new ExportFileNameUtils();
        }
        return instance;
    }

    /**
     * 获取导出文件名
     * @param startTime 开始日期 例如 2021-11-22
     * @param endTime 截至日期 例如 2021-11-22
     * @param type 类型 1:今日 2:昨日 3:最近7天 4:最近30天
     * @param baseFileName 基础文件名
     * @return
     */
    public String getExportFileName(String startTime, String endTime, Integer type, String baseFileName) {
        StringBuilder fileName = new StringBuilder();
        if(type!=null){
            startTime=null;
            endTime=null;
        }
        if(OtherUtills.checkStr(startTime)&&OtherUtills.checkStr(endTime)){
            fileName.append(startTime);
            if(!StringUtils.equals(startTime,endTime)){
                fileName.append(" ~ ").append(endTime);
            }
        }
        if(ObjectUtil.isNotNull(type)){
            fileName = getExportFileNameByType(type);
        }
        fileName.append(" ").append(baseFileName);
        return fileName.toString();
    }

    public StringBuilder getExportFileNameByType(Integer type) {
        StringBuilder fileName = new StringBuilder();
        Integer day = typeMap.get(String.valueOf(type));
        Date endDay = new Date();
        Date startDay = DateUtils.addDays(endDay, day);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        fileName.append(sdf.format(startDay));
        if(type>2) {
            fileName.append(" ~ ").append(sdf.format(endDay));
        }
        return fileName;
    }
}
